package me.zhengjie.hr.employeebase.service.impl;

import me.zhengjie.hr.employeebase.domain.EmployeeBase;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.hr.employeebase.repository.EmployeeBaseRepository;
import me.zhengjie.hr.employeebase.service.EmployeeBaseService;
import me.zhengjie.hr.employeebase.service.dto.EmployeeBaseDto;
import me.zhengjie.hr.employeebase.service.dto.EmployeeBaseQueryCriteria;
import me.zhengjie.hr.employeebase.service.mapper.EmployeeBaseMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author ZGZ
* @date 2020-04-15
*/
@Service
//@CacheConfig(cacheNames = "employeeBase")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class EmployeeBaseServiceImpl implements EmployeeBaseService {

    private final EmployeeBaseRepository employeeBaseRepository;

    private final EmployeeBaseMapper employeeBaseMapper;

    public EmployeeBaseServiceImpl(EmployeeBaseRepository employeeBaseRepository, EmployeeBaseMapper employeeBaseMapper) {
        this.employeeBaseRepository = employeeBaseRepository;
        this.employeeBaseMapper = employeeBaseMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(EmployeeBaseQueryCriteria criteria, Pageable pageable){
        Page<EmployeeBase> page = employeeBaseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(employeeBaseMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<EmployeeBaseDto> queryAll(EmployeeBaseQueryCriteria criteria){
        return employeeBaseMapper.toDto(employeeBaseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public EmployeeBaseDto findById(Integer id) {
        EmployeeBase employeeBase = employeeBaseRepository.findById(id).orElseGet(EmployeeBase::new);
        ValidationUtil.isNull(employeeBase.getId(),"EmployeeBase","id",id);
        return employeeBaseMapper.toDto(employeeBase);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public EmployeeBaseDto create(EmployeeBase resources) {
        return employeeBaseMapper.toDto(employeeBaseRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(EmployeeBase resources) {
        EmployeeBase employeeBase = employeeBaseRepository.findById(resources.getId()).orElseGet(EmployeeBase::new);
        ValidationUtil.isNull( employeeBase.getId(),"EmployeeBase","id",resources.getId());
        employeeBase.copy(resources);
        employeeBaseRepository.save(employeeBase);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            employeeBaseRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<EmployeeBaseDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (EmployeeBaseDto employeeBase : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("姓名", employeeBase.getEmpName());
            map.put("性别", employeeBase.getSex());
            map.put("出生日期", employeeBase.getBirthday());
            map.put("身份证", employeeBase.getIdCard());
            map.put("入所时间", employeeBase.getJoin());
            map.put("籍贯", employeeBase.getNativePlace());
            map.put("民族", employeeBase.getNation());
            map.put("最高学历", employeeBase.getHighestEdu());
            map.put("最高学位", employeeBase.getHighestDegree());
            map.put("学校1（本科）", employeeBase.getFirstSchool());
            map.put("专业1", employeeBase.getFirstMajor());
            map.put("学校2（研究生）", employeeBase.getSecondSchool());
            map.put("专业2", employeeBase.getSecondMajor());
            map.put("职称", employeeBase.getTitle());
            map.put("获得此职称的时间", employeeBase.getTitleBeginTime());
            map.put("专业等级", employeeBase.getProfessionalLevel());
            map.put("手机号", employeeBase.getPhone());
            map.put("联系人", employeeBase.getRelationMember());
            map.put("联系人手机号", employeeBase.getFamilyPhone());
            map.put("家庭住址（详细）", employeeBase.getAddress());
            map.put("婚姻情况", employeeBase.getMaritalStatus());
            map.put("子女情况", employeeBase.getChildrenSituation());
            map.put("政治面貌", employeeBase.getPoliticsStatus());
            map.put("涉密等级", employeeBase.getSecretLevel());
            map.put("工作证编号", employeeBase.getWorkerNumber());
            map.put("邮箱", employeeBase.getEmail());
            map.put("所属中心", employeeBase.getCenterBelong());
            map.put("离职时间", employeeBase.getLeaveTime());
            map.put("离职原因", employeeBase.getLeaveReason());
            map.put("前单位", employeeBase.getFormerUnit());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}